﻿
package extremefx.ui.styles {
	import extremefx.drawing.SlicedBitmap;
	import extremefx.extensions.isNullOrEmpty;
	import extremefx.geom.Edge;
	import extremefx.modules.css.ComputedStyle;
	import extremefx.modules.css.XTextFormat;
	import extremefx.ui.StyleManager;

	import flash.display.BitmapData;

	/**
	 * @author Marcelo Volmaro
	 */
	public final class UIStyle {
		private var _style:ComputedStyle;
		private var _focusImage:SlicedBitmap;
		private var _focusRectangle:Edge;
		
		public var padding:Edge;
		public var margin:Edge;
		public var position:Edge;
		
		public var textFormat:XTextFormat;

		public function UIStyle(pStyle:ComputedStyle) {
			_style = pStyle;
			textFormat = pStyle.getTextStyle();
							
			padding = new Edge(pStyle.value.paddingTop, pStyle.value.paddingRight, pStyle.value.paddingBottom, pStyle.value.paddingLeft);
			margin = new Edge(pStyle.value.marginTop, pStyle.value.marginRight, pStyle.value.marginBottom, pStyle.value.marginLeft);
			position = new Edge(pStyle.value.top, pStyle.value.right, pStyle.value.bottom, pStyle.value.left);
		}
		
		public function getRawProperty(pProperty:String):String {
			return _style.value[pProperty];
		}
		
//		public function getTextStyle(pState:uint = 1):XTextFormat {
		public function getPseudoStyle(pState:uint = 1):UIStyle {
			pState &= 0x7f;//remove pressed
			
			var s:String;
			if (pState & 8) s = "disabled";
			else if (pState & 4) s = "active";
			else if (pState & 2) s = "hover";
			
//			return _style.getPseudoStyle(s).getTextStyle();
			return new UIStyle(_style.getPseudoStyle(s));
		}

		//Tries: asset+state, asset
		private function _getBitmapData(pUrl:String, pState:uint = 1):BitmapInfo {
			var state:String = "n";
			
			if (pState & UIStates.ACTIVE) state ="a";
			else if (pState & UIStates.HOVER) state ="h";
			
			if (pState & UIStates.DISABLED) {
				if (state == "a") {//since focus-disabled does not exists, use that image for active-disabled
					state ="D";
					
				} else {
					state ="d";
				}
			}
			
			if (pState & UIStates.PRESSED) state = state.toUpperCase();
	
			var ret:BitmapInfo;//try first name_state
			if ((ret = StyleManager.getBitmapInfo(_style.value[pUrl], state))) 
				return ret;

			if (pState & UIStates.PRESSED)//uppercase => selected state
				return _getBitmapData(pUrl, pState & 0x7f);
			
			if (!(pState & UIStates.NORMAL)) return _getBitmapData(pUrl, UIStates.NORMAL);//default state
			
			return null;
		}
		
		public function getFirstBMPData():BitmapData {
			var bmp:BitmapInfo = _getBitmapData("borderImageUrl", 1);
			if (bmp == null) bmp = _getBitmapData("backgroundImageUrl", 1);
			return bmp.bmp;
		}
		
		public function getImage(pImageState:uint, pImageName:String = ""):SlicedBitmap {
			if (isNullOrEmpty(pImageName)) pImageName = "borderImageUrl";
			
			var bi:BitmapInfo = _getBitmapData(pImageName, pImageState);			
			if (!bi) return null;
			return new SlicedBitmap(bi.bmp, bi.rx, bi.ry, bi.rec, bi.tx, bi.ty);
		}
		
		public function get focusImage():SlicedBitmap {
			if (_focusImage) return _focusImage;
			var bi:BitmapInfo = StyleManager.getBitmapInfo(_style.getPseudoStyle("focus").value.borderImageUrl, "n");
			_focusImage = bi ? new SlicedBitmap(bi.bmp, bi.rx, bi.ry, bi.rec) : null;
			return _focusImage; 
		}
		
		public function get focusRectangle():Edge {
			if (_focusRectangle) return _focusRectangle;
			
			var o:Object = _style.getPseudoStyle("focus").value;
			_focusRectangle = o ? new Edge(o.top, o.right, o.bottom, o.left) : null;
			return _focusRectangle; 
		}
	}
}
